package com.example.park2212.payer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityPayerDetailBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/payer/PayerDetailActivity")
class PayerDetailActivity : BaseActivity<ActivityPayerDetailBinding,PayerViewModel>() {
    @Autowired
    lateinit var yid :String
    lateinit var payerDetailAdapter: PayerDetailAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.getPayerDetail(yid.toInt())

        payerDetailAdapter = PayerDetailAdapter()
        binding.rvPayerDetail.adapter = payerDetailAdapter
        binding.rvPayerDetail.layoutManager = LinearLayoutManager(this)

        binding.payerDetailCbAll.setOnCheckedChangeListener { compoundButton, b ->
            binding.payerDetailCbAll.setOnClickListener {
                payerDetailAdapter.data.forEach {
                    it.isChecked = b
                }
                payerDetailAdapter.notifyDataSetChanged()
                check()
            }
        }



    }

    private fun check() {
        ////计算合计金额
        var amount = 0f
        payerDetailAdapter.data.forEach {
            if (it.isChecked){
                amount +=it.totalAmount
            }
        }
        binding.tvMoney.text = "￥ $amount"

        ////计算是否全选
        val list = mutableListOf<Boolean>()
        payerDetailAdapter.data.forEach {
            list.add(it.isChecked)
        }
        binding.payerDetailCbAll.isChecked = false !in list
    }


    override fun initData() {
        viewModel.payerDetailSuccess.observe(this){
            binding.bean = it
            payerDetailAdapter.data.clear()
            payerDetailAdapter.addData(it)
            payerDetailAdapter.notifyDataSetChanged()
        }
    }

}